package com.zhixun.mobile.modules.jiujiudevice.device.service;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.ims.modules.jiujiudevice.service.JiuInkService;
import com.zhixun.mobile.modules.jiujiudevice.device.task.Task;
import com.zhixun.mobile.modules.jiujiudevice.device.threadpool.SmartThreadPool;
import com.zhixun.mobile.modules.jiujiudevice.device.util.Hog;

/**
 * 智能设备服务开启
 * @author 郝江波
 *
 */
@Service
public class SocketServerInit {
	
	private static final int port = 8899;
	private static ServerSocket server = null;
	
	
	@Autowired
	public SocketServerInit(final JiuInkService jiuInkService){	
		try {
			server =new ServerSocket(port);
			new Thread(){
				public void run(){
					while(true){
						try {
							if(null != server){
								Hog.i("智能硬件服务端等待连接加入...");
								Socket socket = server.accept();
								SmartThreadPool.execute(new Task(socket,jiuInkService));
							}		
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}.start();
			Hog.e("智能硬件服务端启动成功！端口"+port);
		} catch (IOException e) {
			e.printStackTrace();
			Hog.e("智能硬件服务端启动失败！！");
		}	
	}
	
	
}
